package com.woniuxy.usercoachassemble.controller;


import com.woniuxy.usercoachassemble.entity.CoachCourse;
import com.woniuxy.usercoachassemble.entity.Course;
import com.woniuxy.usercoachassemble.service.CoachCourseService;
import com.woniuxy.usercoachassemble.service.CourseService;
import org.example.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-07-10
 */
@RestController
@RequestMapping("course")
public class CourseController {
    @Autowired
    private CourseService courseServiceImpl;
    @Autowired
    private CoachCourseService coachCourseServiceImpl;
    //查询所有的课程
    @RequestMapping("allcourses")
    public Result allcourses(HttpServletRequest request){
        int  coachId=request.getIntHeader("userId");
        List<Course> courses=courseServiceImpl.query().list();
        for(Course course: courses){
            List<CoachCourse> list = coachCourseServiceImpl.query().eq("course_id", course.getCourseId()).eq("coach_id", coachId).list();
            if (list.size()==0){
                course.setIshave("0");
            }else {

                course.setIshave("1");
            }


        }
        return  Result.Success(courses);

    }

    @RequestMapping("addtomycourse")
    public Result addtomycourse(int courseId ,HttpServletRequest request){
        CoachCourse coachCourse = new CoachCourse();
        coachCourse.setCourseId(courseId);
        coachCourse.setCoachId(request.getIntHeader("userId"));
        coachCourseServiceImpl.save(coachCourse);
        return  Result.Success(coachCourse);
    }

}

